package ubi.quando.UI;

import java.util.Calendar;
import java.util.Date;

import ubi.quando.App.EventBase;
import ubi.quando.App.EventType;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public abstract class ArrangeEventBaseActivity extends Activity {

	static final int TIME_DIALOG_ID = 0;
	static final int DATE_DIALOG_ID = 1;

	public static final String SerializeIsDest = "ChooseDestination";

	protected EventBase mEvent;
	protected UbiApplication mApp;

	protected TextView mTvWhen;
	protected TextView mTvWhere;
	protected TextView mTvAt;
	protected TextView mTvTo;
	protected TextView mDateDisplay;
	protected Button mPickDateBtn;
	protected TextView mTimeDisplay;
	protected Button mPickTimeBtn;
	protected TextView mLocationDisplay;
	protected Button mChooseLocationBtn;
	protected TextView mDestinationDisplay;
	protected Button mChooseDestinationBtn;
	protected Button mAddParticipantsBtn;
	protected Button mConfirmBtn;
	protected Button mCancelBtn;
	protected EditText mEventName;
	protected LinearLayout mLoutName;
	protected LinearLayout mLoutDate;
	protected LinearLayout mLoutTime;
	protected LinearLayout mLoutDest;
	protected LinearLayout mLoutLoc;
	protected LinearLayout mLoutParticipants;
	protected TextView mTvEventName;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.event_create);

		// get the current date
		final Calendar c = Calendar.getInstance();

		mApp = (UbiApplication) getApplication();
		mApp.setCurrentActivity(this);

		InitializeEvent();

		Date date = mEvent.GetDate();

		date.setYear(c.get(Calendar.YEAR));
		date.setMonth(c.get(Calendar.MONTH));
		date.setDate(c.get(Calendar.DAY_OF_MONTH));
		// get the current time
		date.setHours(c.get(Calendar.HOUR_OF_DAY));
		date.setMinutes(c.get(Calendar.MINUTE));

		InitViewMembers();

		InitListeners();

		initFont();
		
		// display the current date
		updateDisplay();
	}

	@Override
	protected void onResume() {
		mEventName.setText(mApp.getTempEventName());
		super.onResume();
	}

	@Override
	protected void onPause() {
		mApp.setTempEventName(mEventName.getText().toString());
		super.onPause();
	}

	private void InitListeners() {

		mPickDateBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		mPickTimeBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID);
			}
		});

		mChooseDestinationBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ChooseDestination(v);
			}
		});

		mChooseLocationBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ChooseLocation(v);
			}
		});

		mAddParticipantsBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				AddParticipants(v);
			}
		});

		mConfirmBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ConfirmEvent(v);
			}
		});

		mCancelBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				CancelEvent(v);
			}
		});
	}

	private void initFont() {
		Typeface font = Typeface.createFromAsset(getAssets(), "AGENCYR.TTF");
		mTvEventName.setTypeface(font);
		mPickDateBtn.setTypeface(font);
		mPickTimeBtn.setTypeface(font);
		mChooseDestinationBtn.setTypeface(font);
		mChooseLocationBtn.setTypeface(font);
		mAddParticipantsBtn.setTypeface(font);
		mConfirmBtn.setTypeface(font);
		mCancelBtn.setTypeface(font);
		mLocationDisplay.setTypeface(font);
		mTvWhen.setTypeface(font);
		mTvWhere.setTypeface(font);
		mTvAt.setTypeface(font);
		mTvTo.setTypeface(font);
		mDateDisplay.setTypeface(font);
		mTimeDisplay.setTypeface(font);
		mDestinationDisplay.setTypeface(font);
		mEventName.setTypeface(font);
	}

	private void InitViewMembers() {
		mTvEventName = (TextView) findViewById(R.id.tv_eventName);
		mPickDateBtn = (Button) findViewById(R.id.pickDate);
		mPickTimeBtn = (Button) findViewById(R.id.pickTime);
		mChooseDestinationBtn = (Button) findViewById(R.id.choose_destination_button);
		mChooseLocationBtn = (Button) findViewById(R.id.choose_location_button);
		mAddParticipantsBtn = (Button) findViewById(R.id.add_participants_button);
		mConfirmBtn = (Button) findViewById(R.id.btn_confirm);
		mCancelBtn = (Button) findViewById(R.id.btn_cancel);
		mLocationDisplay = (TextView) findViewById(R.id.choose_location_txt);
		mTvWhen = (TextView) findViewById(R.id.when);
		mTvWhere = (TextView) findViewById(R.id.where);
		mTvAt = (TextView) findViewById(R.id.at);
		mTvTo = (TextView) findViewById(R.id.to);
		mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
		mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
		mDestinationDisplay = (TextView) findViewById(R.id.choose_destination_txt);
		mEventName = (EditText) findViewById(R.id.txt_eventName);

		mLoutName = (LinearLayout) findViewById(R.id.lout_eventName);
		mLoutDate = (LinearLayout) findViewById(R.id.lout_date);
		mLoutTime = (LinearLayout) findViewById(R.id.lout_time);
		mLoutDest = (LinearLayout) findViewById(R.id.lout_dest);
		mLoutLoc = (LinearLayout) findViewById(R.id.lout_loc);
		mLoutParticipants = (LinearLayout) findViewById(R.id.lout_participants);
	}

	protected void updateDisplay() {
		Date date = mEvent.GetDate();

		mDateDisplay.setText(new StringBuilder()
				// Month is 0 based so add 1
				.append(date.getMonth() + 1).append("-").append(date.getDate())
				.append("-").append(date.getYear()).append(" "));
		mTimeDisplay.setText(new StringBuilder().append(pad(date.getHours()))
				.append(":").append(pad(date.getMinutes())));

		if (mEvent.GetDestination() != null) {
			mDestinationDisplay.setText(mEvent.getdestName());
		} else {
			mDestinationDisplay.setText("No destination");
		}
	}

	// the callback received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			Date date = mEvent.GetDate();

			date.setYear(year);
			date.setMonth(monthOfYear);
			date.setDate(dayOfMonth);

			updateDisplay();
		}
	};

	// the callback received when the user "sets" the time in the dialog
	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

			Date date = mEvent.GetDate();

			// get the current time
			date.setHours(hourOfDay);
			date.setMinutes(minute);

			updateDisplay();
		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		Date date = mEvent.GetDate();

		switch (id) {
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, mTimeSetListener,
					date.getHours(), date.getMinutes(), false);
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, date.getYear(),
					date.getMonth(), date.getDate());
		}
		return null;
	}

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	protected void ChooseDestination(View v) {
		Intent chooseLoc = new Intent(v.getContext(),
				ChooseLocationActivity.class);

		Bundle b = new Bundle();
		b.putBoolean(SerializeIsDest, true);
		SetEventType(b);

		saveEventToBundle(b);
		chooseLoc.putExtras(b);

		startActivity(chooseLoc);
	}

	private void ChooseLocation(View v) {
		Intent chooseLoc = new Intent(v.getContext(),
				ChooseLocationActivity.class);

		Bundle b = new Bundle();
		b.putBoolean(SerializeIsDest, false);
		SetEventType(b);

		saveEventToBundle(b);
		chooseLoc.putExtras(b);

		startActivity(chooseLoc);
	}

	protected void saveEventToBundle(Bundle b) {
		b.putSerializable(EventBase.SerializeName, mEvent);
	}

	protected void saveEventTypeToBundle(EventType type, Bundle b) {
		b.putInt(EventType.SerializeName, EventType.ToInt(type));
	}

	protected abstract void AddParticipants(View v);

	protected abstract void InitializeEvent();

	protected abstract void SetEventType(Bundle b);

	protected void ConfirmEvent(View v) {
		ReturnToMenu(v);
	}

	protected void ReturnToMenu(View v) {
		mEventName.setText("");
		Intent intent = new Intent(getApplicationContext(), MainMenu.class);
		startActivity(intent);
	}

	protected boolean IsEventValid() {
		String name = mEventName.getText().toString();
		if (name.equals("")) {
			Toast.makeText(getApplicationContext(),
					"You must define name for the event", Toast.LENGTH_LONG)
					.show();
			return false;
		}
		mEvent.SetName(name);

		if (mEvent.GetDestination() == null) {
			Toast.makeText(getApplicationContext(),
					"You must define events destination", Toast.LENGTH_LONG)
					.show();
			return false;
		}
		return true;
	}

	private void CancelEvent(View v) {
		mEvent = null;
		ReturnToMenu(v);
	}
}